//
//  AddressBookCell.swift
//  Employee
//
//  Created by swordjoy on 2017/3/22.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class AddressBookCell: UITableViewCell {
  static var identifier: String { return "swxc.ConversationCell" }
  
  static var height: CGFloat { return 95 }
  
  // MARK: - property
  
  var chatUser: ChatUser? {
    didSet {
      guard let model = chatUser else { return }
      
      headIcon.setImage(with: model.headIconUrl, placeholder: GlobalConfig.DefaultHeadIconImage)
      nickname.text = model.nickname
      phoneNumber.text = model.phoneNumber
      department.text = model.department
    }
  }
  
  fileprivate var contentV: UIView!
  
  fileprivate var headIcon: UIImageView!
  fileprivate var nickname: UILabel!
  fileprivate var phoneNumber: UILabel!
  fileprivate var department: UILabel!
  
    fileprivate var callPhoneBtn: UIButton!
    
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    backgroundColor = UIColor.clear
    createLayout()
  }
  
  override func layoutSubviews() {
    super.layoutSubviews()
    
    contentView.sj.width = UIScreen.width
    
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

// MARK: - layout
extension AddressBookCell {
  
  func createLayout() {
    
    contentV = UIView()
    contentView.addSubview(contentV)
    contentV.backgroundColor = UIColor.white
    contentV.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.top.equalToSuperview()
      make.right.equalTo(-10)
      make.bottom.equalTo(-5)
    }
    contentV.layer.cornerRadius = 4
    contentV.clipsToBounds = true
    
    headIcon = UIImageView()
    contentV.addSubview(headIcon)
    headIcon.image = GlobalConfig.PlaceholderImage
    headIcon.snp.makeConstraints { (make) in
      make.height.width.equalTo(50)
      make.centerY.equalToSuperview()
      make.left.equalTo(10)
    }
    headIcon.layer.cornerRadius = 4
    headIcon.clipsToBounds = true
    
    nickname = UILabel()
    contentV.addSubview(nickname)
    nickname.font = GlobalConfig.system16Font
    nickname.textColor = GlobalConfig.TextDeepColor
    nickname.snp.makeConstraints { (make) in
      make.left.equalTo(headIcon.snp.right).offset(12)
      make.top.equalTo(19)
    }
    
    phoneNumber = UILabel()
    contentV.addSubview(phoneNumber)
    phoneNumber.font = GlobalConfig.system14Font
    phoneNumber.textColor = GlobalConfig.TextLightColor
    phoneNumber.snp.makeConstraints { (make) in
      make.left.equalTo(headIcon.snp.right).offset(12)
      make.top.equalTo(nickname.snp.bottom).offset(9)
    }
    
    department = UILabel()
    contentV.addSubview(department)
    department.font = GlobalConfig.system14Font
    department.textColor = GlobalConfig.TextLightColor
    department.snp.makeConstraints { (make) in
      make.centerY.equalTo(nickname.snp.centerY)
      make.right.equalTo(-57)
    }
    
    callPhoneBtn = UIButton(type: .custom)
    callPhoneBtn.setImage(UIImage(named:"callPhone"), for: .normal)
    callPhoneBtn.addTarget(self, action: #selector(call), for: .touchUpInside)
    contentV.addSubview(callPhoneBtn)
    callPhoneBtn.snp.makeConstraints { (make) in
        make.centerY.equalTo(contentV.snp.centerY)
        make.right.equalTo(-20)
    }
  }
    
    
    @objc func call() {
        
        guard let phone = chatUser?.phoneNumber else {
            return
        }
        
        let phoneUrlRequest = URLRequest(url: URL(string: "tel:"+phone)!)
        
        let webView = UIWebView()
        webView.loadRequest(phoneUrlRequest)
        SJApp.keyWindow.addSubview(webView)
    }
    
}
